//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "CRefMultiple.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
//---------------------------------------------------------------------------
//              CONSTRUCTEUR
//---------------------------------------------------------------------------
__fastcall TRefMultiple::TRefMultiple(  TComponent* Owner,
                                            const int NbRef,
                                            const int Logiciel,
                                            const int Version,
                                            const AnsiString RefNom,
                                            const AnsiString RefDesignation,
                                            const AnsiString RefAdresse)
    : TForm(Owner)
{
    lEntete->Caption = PAGE_MULTIPLE;
    //Initialiser les variables
    //-------------------------
    nIndexPanel = 0;
    m_nLogiciel = Logiciel;
    m_nVersion  = Version;
    m_asRefNom   = RefNom;

    AfficherRefMultiple(NbRef);
    TZZMySqlQuery * MaRequete = NULL;
    MaRequete = new TZZMySqlQuery( (TComponent*)0);
    MaRequete->Database = dmWebHelp->dbWebHelp;

    MaRequete->Sql->Text =  (AnsiString)"select log_nom,"+       //0
                                                "ver_nom,"+      //1
                                                "ref_sous_nom,"+ //2
                                                "ref_adresse "+  //3
                                        "from refer "+
                                            "left outer join logiciels on "+
                                                "(refer.log_code = logiciels.log_code) "+
                                            "inner join versions on "+
                                                "(refer.ver_code = versions.ver_code) "+
                                        "where (refer.log_code = "+Logiciel+" and refer.ver_code = "+Version+") and ref_nom = \'"+RefNom+"\'";
    MaRequete->Open();
    MaRequete->First();
    eLogiciel->Text    = MaRequete->Fields->Fields[0]->AsString;
    eVersion->Text     = MaRequete->Fields->Fields[1]->AsString;
    eReference->Text   = RefNom;
    ePageWeb->Text     = RefAdresse;
    if(!RefDesignation.IsEmpty())
        eDesignation->Text = RefDesignation;
    else
        eDesignation->Color = clRed;

    while(!MaRequete->Eof) {
        RenseignerChamp(MaRequete->Fields->Fields[2]->AsString, MaRequete->Fields->Fields[3]->AsString);
        MaRequete->Next();
    }

    MaRequete->Sql->Clear();
    MaRequete->Close();
    if(MaRequete) {delete MaRequete; MaRequete = NULL;}
}
//---------------------------------------------------------------------------
//              AFFICHER REFERENCE MULTIPLE
//---------------------------------------------------------------------------
void __fastcall TRefMultiple::AfficherRefMultiple( const int NbreRef )
{
    TPanel * Panel = NULL;
    for(int i=0; i<(NbreRef); i++) {
        Panel               = new TPanel(pReste);
        Panel->Name         = (AnsiString)"Panel"+i;
        AfficherZonePanel( Panel );
    }

    //Calcul du Range pour la ScrollBox
    //---------------------------------
    sbAdresseMultiple->VertScrollBar->Range = 25 + (80*(NbreRef));
}
//---------------------------------------------------------------------------
//              STRUCTURE ZONE PANEL
//---------------------------------------------------------------------------
void __fastcall TRefMultiple::AfficherZonePanel( TPanel * Panel )
{
    TEdit       * Designation;
    TEdit       * PageWeb;
    TLabel      * lDesignation;
    TLabel      * lPageWeb;
    TLabel      * lAttente;
    TComboBox   * cbAttente;
    TButton     * bConsulter;

    Panel->Parent       = pReste;
    Panel->Align        = alTop;
    Panel->Width        = 502;
    Panel->Height       = 80;
    Panel->Color        = clMenu;

    PageWeb             = new TEdit(Panel);
    Designation         = new TEdit(Panel);
    cbAttente           = new TComboBox(Panel);
    lDesignation        = new TLabel(Panel);
    lPageWeb            = new TLabel(Panel);
    lAttente            = new TLabel(Panel);
    bConsulter          = new TButton(Panel);

    //Designation
    //-----------
    Designation->Parent = Panel;
    Designation->Left   = 112;
    Designation->Top    = 4;
    Designation->Width  = 382;
    Designation->Height = 21;
    Designation->Color  = clWhite;
    Designation->Name   = "Designation";
    Designation->Text   = "";

    lDesignation->Parent= Panel;
    lDesignation->Caption = "Désignation";
    lDesignation->Font->Style = TFontStyles()<< fsBold << fsUnderline;
    lDesignation->Left  = 8;
    lDesignation->Top   = 4;

    //Page Web
    //--------
    PageWeb->Parent     = Panel;
    PageWeb->Name       = "PageWeb";
    PageWeb->Left       = 112;
    PageWeb->Top        = 28;
    PageWeb->Width      = 382;
    PageWeb->Height     = 21;
    PageWeb->Color      = clWhite;
    PageWeb->Text       = "";
    PageWeb->ReadOnly   = true;

    lPageWeb->Parent    = Panel;
    lPageWeb->Caption   = "Page Web";
    lPageWeb->Left      = 8;
    lPageWeb->Top       = 28;

    bConsulter->Parent  = Panel;
    bConsulter->Name    = "Consulter";
    bConsulter->Caption = "";
    bConsulter->Left    = 72;
    bConsulter->Top     = 21;
    bConsulter->Height  = 20;
    bConsulter->Width   = 25;


    //Page d'attente
    //--------------
    cbAttente->Parent     = Panel;
    cbAttente->Left       = 112;
    cbAttente->Top        = 52;
    cbAttente->Width      = 382;
    cbAttente->Height     = 21;
    cbAttente->Items->Add("En cours de mise à jour");
    cbAttente->Items->Add("Non disponible");

    lAttente->Parent     = Panel;
    lAttente->Caption    = "Page d'attente";
    lAttente->Left       = 8;
    lAttente->Top        = 52;
}
//---------------------------------------------------------------------------
//              UTILISATION DES COMPOSANTS DYNAMIQUE
//---------------------------------------------------------------------------
void __fastcall TRefMultiple::RenseignerChamp( const AnsiString Designation, const AnsiString PageWeb)
{
    //Permet de nous informer sur le nom du panel utilisé
    //---------------------------------------------------
    TComponent * pChildPanel = pReste->Components[nIndexPanel];

    //Composant fils du panel
    //-----------------------
    for(int IndexFilsPanel=0;IndexFilsPanel<((TPanel*)pChildPanel)->ComponentCount;IndexFilsPanel++) {

        TComponent * pChildFilsPanel  = pChildPanel->Components[IndexFilsPanel];

        if( dynamic_cast<TEdit*>(pChildFilsPanel)) {
            if( ((TEdit*)pChildFilsPanel)->Name == "Designation") {
                if(!Designation.IsEmpty())
                    ((TEdit*)pChildFilsPanel)->Text = Designation;
                else
                    ((TEdit*)pChildFilsPanel)->Color = clRed;

            }

            if( ((TEdit*)pChildFilsPanel)->Name == "PageWeb") {
                ((TEdit*)pChildFilsPanel)->Text = PageWeb;
            }
        }

        if( dynamic_cast<TButton*>(pChildFilsPanel)) {
            if( ((TButton*)pChildFilsPanel)->Name == "Consulter") {
                ((TButton*)pChildFilsPanel)->OnClick = AfficherPageWeb;
            }
        }
    }
    //Prochain panel
    //--------------
    nIndexPanel++;
}
//---------------------------------------------------------------------------
//          AFFICHER PAGE WEB
//---------------------------------------------------------------------------
void __fastcall TRefMultiple::bConsulterClick(TObject *Sender)
{
    ShellExecute(NULL, "Open", ePageWeb->Text.c_str(),NULL, NULL, SW_SHOWMAXIMIZED);
}
//---------------------------------------------------------------------------
//          AFFICHER PAGE WEB VENANT DES BOUTONS DYNAMIQUE
//---------------------------------------------------------------------------
void __fastcall TRefMultiple::AfficherPageWeb(TObject *Sender)
{
    AnsiString Adresse = "";
    //Recuperation du bouton appuyé
    //-----------------------------
    TButton * MonBouton = dynamic_cast<TButton*>(Sender);
    //Parent du bouton
    //----------------
    TPanel * MonPanel = dynamic_cast<TPanel*>(MonBouton->Parent);

    for(int i=0; i<MonPanel->ComponentCount;i++) {
        TComponent * pFils  = MonPanel->Components[i];
        if( dynamic_cast<TEdit*>(pFils)) {
            if( ((TEdit*)pFils)->Name == "PageWeb") {
                Adresse = ((TEdit*)pFils)->Text;
            }
        }
    }
    if(!Adresse.IsEmpty())
        ShellExecute(NULL, "Open", Adresse.c_str(), NULL, NULL, SW_SHOWMAXIMIZED);
}
//---------------------------------------------------------------------------
//                          VALIDATION
//---------------------------------------------------------------------------
void __fastcall TRefMultiple::bbOkClick(TObject *Sender)
{
    if( VerifierDesignation() ) {
        //Recuperer toutes les designations
        //---------------------------------
        queue<AnsiString> qDesignation;

        //FIFO temporaire permettant la récupération du ref_code associé à une référence et une adresse
        //---------------------------------------------------------------------------------------------
        queue<AnsiString> qTempPageWeb;

        //Remplissage des FIFO qDesignation et qPageWeb
        //---------------------------------------------
        for(int i=0; i<pReste->ComponentCount; i++) {
            TComponent * pChildPanel = pReste->Components[i];
            for(int IndexFilsPanel=0;IndexFilsPanel<((TPanel*)pChildPanel)->ComponentCount;IndexFilsPanel++) {
                TComponent * pChildFilsPanel  = pChildPanel->Components[IndexFilsPanel];
                if( dynamic_cast<TEdit*>(pChildFilsPanel)) {

                    if( ((TEdit*)pChildFilsPanel)->Name == "Designation") {
                        qDesignation.push(((TEdit*)pChildFilsPanel)->Text);
                    }

                    if( ((TEdit*)pChildFilsPanel)->Name == "PageWeb") {
                        qTempPageWeb.push(((TEdit*)pChildFilsPanel)->Text);
                    }
                }

            }
        }

        queue<int> qRefCode;
        //Remplissage de la FIFO qRefCode
        //-------------------------------
        while(!qTempPageWeb.empty()) {
            qRefCode.push( GetRefCode(qTempPageWeb.front()) );
            qTempPageWeb.pop();
        }

        //Insertion dans la base WebHelp
        //------------------------------
        dmWebHelp->tRefer->Active = true;
        dmWebHelp->tRefer->Insert();
        ntRefer(LOG_CODE)       = m_nLogiciel;
        ntRefer(VER_CODE)       = m_nVersion;
        astRefer(REF_NOM)       = m_asRefNom;
        astRefer(REF_SOUS_NOM)  = eDesignation->Text;
        astRefer(REF_ADRESSE)   = ePageWeb->Text;
        dmWebHelp->tRefer->Post();
        dmWebHelp->trWebHelp->Commit();

        //Mise à jour des champ désignation
        //---------------------------------
        dmWebHelp->tRefer->Active = true;


        while(   !qRefCode.empty()
              && !qDesignation.empty()){

            dmWebHelp->tRefer->First();
            while(!dmWebHelp->tRefer->Eof) {
                if(ntRefer(REF_CODE) == qRefCode.front()) {
                    dmWebHelp->tRefer->Edit();
                    astRefer(REF_SOUS_NOM)  = qDesignation.front();
                    dmWebHelp->tRefer->Post();
                    dmWebHelp->trWebHelp->Commit();
                    
                    qRefCode.pop();
                    qDesignation.pop();
                    break;
                }
                dmWebHelp->tRefer->Next();
            }
        }
        //Validation OK
        //-------------
        bbOk->ModalResult = mrOk;
        Close();
    }
    else {
        bbOk->ModalResult = mrNone;
    }
}
//---------------------------------------------------------------------------
//                  RECUPERATION DU REF_CODE
//---------------------------------------------------------------------------
int  __fastcall TRefMultiple::GetRefCode( const AnsiString PageWeb )
{
    int RefCode = 0;

    //Recuperer le ref_code permettant d'inserer au bon endroit la désignation
    //------------------------------------------------------------------------
    TZZMySqlQuery * MaRequete = NULL;
    MaRequete = new TZZMySqlQuery( (TComponent*)0);
    MaRequete->Database = dmWebHelp->dbWebHelp;

    MaRequete->Sql->Text =  (AnsiString)"select ref_code "+
                                        "from refer where (log_code="+m_nLogiciel+" and ver_code="+m_nVersion+") "+
                                        "and (ref_nom=\'"+m_asRefNom+"\' and ref_adresse=\'"+PageWeb+"\')";
    MaRequete->Open();
    MaRequete->First();

    RefCode = MaRequete->Fields->Fields[0]->AsInteger;

    MaRequete->Sql->Clear();
    MaRequete->Close();
    if(MaRequete) {delete MaRequete; MaRequete = NULL;}

    return RefCode;
}
//---------------------------------------------------------------------------
//                  VERIFIER LES DESIGNATIONS NON REMPLIES
//---------------------------------------------------------------------------
bool __fastcall TRefMultiple::VerifierDesignation( void )
{
    bool bRetour = false;
    queue<bool> qVerif;

    if(!eDesignation->Text.IsEmpty()) {
        eDesignation->Color = clWhite;
        qVerif.push(true);
    }

    else {
        eDesignation->Color = clRed;
        qVerif.push(false);
    }
        
    for(int i=0; i<pReste->ComponentCount; i++) {
        TComponent * pChildPanel = pReste->Components[i];
        for(int IndexFilsPanel=0;IndexFilsPanel<((TPanel*)pChildPanel)->ComponentCount;IndexFilsPanel++) {
            TComponent * pChildFilsPanel  = pChildPanel->Components[IndexFilsPanel];
            if( dynamic_cast<TEdit*>(pChildFilsPanel)) {

                if( ((TEdit*)pChildFilsPanel)->Name == "Designation") {
                    if( !((TEdit*)pChildFilsPanel)->Text.IsEmpty() ) {
                        ((TEdit*)pChildFilsPanel)->Color = clWhite;
                        qVerif.push(true);
                    }
                    else {
                        ((TEdit*)pChildFilsPanel)->Color = clRed;
                        qVerif.push(false);
                    }
                }
            }

        }
    }

    while(!qVerif.empty()) {
        if(!qVerif.front()) {
            bRetour = false;
            break;
        }
        else
            bRetour = true;
       qVerif.pop();
    }
    return bRetour;
}
